﻿using BeetleX.XRPC.Clients;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            TestChat();
            while(!Stoped)
            {
                Thread.Sleep(1000);
                Console.WriteLine("心跳");
            }
        }

        static bool Stoped = false;

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
        }

        private static async void TestChat()
        {
            await Task.Run(() =>
            {
                XRPCClient client = new XRPCClient("localhost", 9999, 2);
                client.NetError = new BeetleX.Clients.EventClientError((client, e) =>
                {
                    if (e.Error != null)
                        Console.WriteLine(e.Error.Message);
                    if (!string.IsNullOrEmpty(e.Message))
                        Console.WriteLine(e.Message);
                });
                client.Connect();
                Console.WriteLine("客户端启动成功！");
                Console.WriteLine("你是?");
                string token = Guid.NewGuid().ToString();
                string line = Console.ReadLine();

            });
        }

        private static void StartXRpcClient()
        {
            XRPCClient client = new XRPCClient("localhost", 9999);
            client.NetError = new BeetleX.Clients.EventClientError((client, e) =>
            {
                if (e.Error != null)
                    Console.WriteLine(e.Error.Message);
                if (!string.IsNullOrEmpty(e.Message))
                    Console.WriteLine(e.Message);
            });
            client.Connect();
            string token = Guid.NewGuid().ToString();
            Console.WriteLine($"正在启动客户端{token}");



            Task.Run(() => {
                IChatService service = client.Create<IChatService>(token);
                Console.WriteLine($"启动了客户端{token}");
                do
                {
                    string text = Guid.NewGuid().ToString();
                    Console.WriteLine($"{token} want say {text}!");
                    service.Say(token, text);
                    Console.WriteLine($"{token} want said {text}!");
                    Thread.Sleep(1000);
                }
                while (true);
            });
        }
    }
}
